<template>
    <el-form label-width="120px" style="margin-top: 20px;">
        <el-form-item label="精选题库广告：">
            <div class="avatar-uploader" @click="openImageDialog('img')">
                <el-image v-if="settingData[0] && settingData[0].keyValue" :src="settingData[0].keyValue" class="avatar"
                    fit="contain"></el-image>
                <i v-else class="el-icon-plus avatar-uploader-icon"><el-icon>
                        <Plus />
                    </el-icon></i>
                <div v-if="settingData[0] && settingData[0].keyValue" class="mask">替换</div>
                <el-icon v-if="settingData[0] && settingData[0].keyValue" class="del-image" :size="20"
                    @click.stop="handleDelImg('img')">
                    <CircleCloseFilled />
                </el-icon>
            </div>
        </el-form-item>
        <el-form-item label="考前密卷广告：">
            <div class="avatar-uploader" @click="openImageDialog('imgTwo')">
                <el-image v-if="settingData[1] && settingData[1].keyValue" :src="settingData[1].keyValue" class="avatar"
                    fit="contain"></el-image>
                <i v-else class="el-icon-plus avatar-uploader-icon"><el-icon>
                        <Plus />
                    </el-icon></i>
                <div v-if="settingData[1] && settingData[1].keyValue" class="mask">替换</div>
                <el-icon v-if="settingData[1] && settingData[1].keyValue" class="del-image" :size="20"
                    @click.stop="handleDelImg('imgTwo')">
                    <CircleCloseFilled />
                </el-icon>
            </div>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" style="width: 90px" @click="handleSave" :loading="btnLoding">
                保存
            </el-button>
        </el-form-item>
    </el-form>
    <image-selected-dialog v-if="showImageChooseDialog" :single-type="selectImageSingle"
        :show-image-choose-dialog="showImageChooseDialog" @close-dialog="handleCloseImageDialog"
        :operationType="false"></image-selected-dialog>
</template>
<script lang="ts" setup>
import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'
import { getBasicInfo, addBasicInfo } from '@/api/materialManagement'

const showImageChooseDialog = ref(false)
const selectImageSingle = ref(true)
const currentImageType = ref('img') // 'img' 或 'imgTwo'
const openImageDialog = (type: string) => {
    currentImageType.value = type
    showImageChooseDialog.value = true
}

const btnLoding = ref(false)

const handleCloseImageDialog = (value: any) => {
    if (value && value.fileUrl) {
        if (currentImageType.value === 'img') {
            settingData.value[0].keyValue = value.fileUrl
        } else if (currentImageType.value === 'imgTwo') {
            settingData.value[1].keyValue = value.fileUrl
        }
    }
    showImageChooseDialog.value = false
}
const handleDelImg = (e: string) => {
    if (e === 'img') {
        settingData.value[0].keyValue = ''
    } else {
        settingData.value[1].keyValue = ''
    }
}

const settingData = ref([
    {
        id: '',
        sdkTypeCode: "SHARE",
        keyCode: "QUESTION_URL",
        keyValue: "",
        sensitive: 0,
        status: 1,
        sort: 3,
        remark: "小程序分享设置-装修-广告图-精选题库广告"
    },
    {
        id: '',
        sdkTypeCode: "SHARE",
        keyCode: "EXAM_URL",
        keyValue: "",
        sensitive: 0,
        status: 1,
        sort: 4,
        remark: "小程序分享设置-装修-广告图-考前密卷广告"
    }
])
const getData = async () => {
    const res = await getBasicInfo({
        sdkTypeCode: 'SHARE'
    })
    if (res.code === '00000' && res.data) {
        res.data.aoList.forEach((item: any) => {
            const target = settingData.value.find(s => s.keyCode === item.keyCode)
            if (target) {
                target.keyValue = item.keyValue
                target.id = item.id
            }
        })
    }
}

const handleSave = async () => {
    try {
        btnLoding.value = true
        const res = await addBasicInfo(settingData.value)
        if (res.code === '00000') {
            ElMessage.success('操作成功')
            getData()
        }
    } catch { } finally {
        btnLoding.value = false
    }
}

onMounted(() => {
    getData()
})
</script>
<style lang="scss" scoped>
.avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 12px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 160px;
    height: 160px;
    background-color: #fff;
    text-align: center;
    line-height: 160px;

    .mask {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 24px;
        background-color: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
        opacity: 0;
        transition: opacity 0.3s ease;
        color: #fff;
        font-size: 12px;
    }

    .del-image {
        display: none;
        position: absolute;
        top: 0;
        right: 0;
        cursor: pointer;
        color: #8c939d;
    }

    &:hover .mask {
        opacity: 1;
    }

    &:hover .del-image {
        display: block;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
    }
}
</style>